package com.lpit.service.impl;

import com.lpit.dto.ArticleHomeDto;
import com.lpit.result.ResponseResult;
import com.lpit.pojo.Article;
import com.lpit.mapper.ArticleMapper;
import com.lpit.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Administrator
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public ResponseResult<List<Article>> selectList(ArticleHomeDto article) {
        if (null == article) {
            article = new ArticleHomeDto();
        }

        if (null == article.getNewestTime()) {
            //.setArticleTime(new Date());
        }

        List<Article> articleList = articleMapper.selectList(null);
        if(null == articleList || 0 == articleList.size()){
            return new ResponseResult<>(404,null,"查询失败");
        }
        return new ResponseResult<>(articleList);
    }

    @Override
    public ResponseResult<List<ArticleHomeDto>> selectHomePageDisplay() {
        List<ArticleHomeDto> articleList = articleMapper.selectHomePageDisplay();
        if(null == articleList || 0 == articleList.size()){
            return new ResponseResult<>(404,null,"查询失败");
        }
        return new ResponseResult<>(articleList);
    }

    @Override
    public ResponseResult<Integer> addArticle(Article article) {
        return articleMapper.insert(article) > 0 ? new ResponseResult<Integer>(null) : new ResponseResult<Integer>(404, null, "添加失败");
    }

    /**累计文章阅读数*/
    @Override
    public ResponseResult<Long> getReadSum() {
        Long aLong = articleMapper.sumNumberArticlesRead();
        return new ResponseResult<>(aLong);
    }
}
